﻿namespace mok4;

class Program
{
    static void Main(string[] args)
    {
        
        //有符号类型
        /*Console.WriteLine("Signed integral types:");

        Console.WriteLine($"sbyte  : {sbyte.MinValue} to {sbyte.MaxValue}");
        Console.WriteLine($"short  : {short.MinValue} to {short.MaxValue}");
        Console.WriteLine($"int    : {int.MinValue} to {int.MaxValue}");
        Console.WriteLine($"long   : {long.MinValue} to {long.MaxValue}");
        Console.WriteLine("");
        Console.WriteLine("Unsigned integral types:");*/

        //无符号类型
        /*Console.WriteLine($"byte   : {byte.MinValue} to {byte.MaxValue}");
        Console.WriteLine($"ushort : {ushort.MinValue} to {ushort.MaxValue}");
        Console.WriteLine($"uint   : {uint.MinValue} to {uint.MaxValue}");
        Console.WriteLine($"ulong  : {ulong.MinValue} to {ulong.MaxValue}");*/
        
        
        /*Console.WriteLine("");
        Console.WriteLine("Floating point types:");
        Console.WriteLine($"float  : {float.MinValue} to {float.MaxValue} (with ~6-9 digits of precision)");
        Console.WriteLine($"double : {double.MinValue} to {double.MaxValue} (with ~15-17 digits of precision)");
        Console.WriteLine($"decimal: {decimal.MinValue} to {decimal.MaxValue} (with 28-29 digits of precision)");*/
        
        //由于任何 int 值都可以轻松地纳入 decimal，因此编译器会执行转换。
        /*int myInt = 3;
        Console.WriteLine($"int: {myInt}");

        decimal myDecimal = myInt;
        
        Console.WriteLine($"decimal: {myDecimal}");*/
        // decimal myDecimal2 = 3.12m;
        //
        // Console.WriteLine($"decimal2: {myDecimal2}");
        // int mtInt2 = (int)myDecimal2;
        // Console.WriteLine($"int: {mtInt2}");

        //使用 ToString() 将数字转换为 string
        int a = 1;
        int b = 2;
        string s = a.ToString()+b.ToString();
        // Console.WriteLine(s);
        
        //使用 string 帮助程序方法将 int 转换为 Parse()

        string s1 = "1";
        string s2 = "1";
        int sum = int.Parse(s1) + int.Parse(s2);
        // Console.WriteLine(sum);
        
        //使用 string 类将 int 转换为 Convert
        // string value1 = "5";
        // string value2 = "7";
        // int result = Convert.ToInt32(value1) * Convert.ToInt32(value2);
        // Console.WriteLine(result);
        
        //比较强制转换和将 decimal 转换为 int
        // int value = (int)1.5m;
        // Console.WriteLine(value);
        
        // int value3 = Convert.ToInt32(1.5m);
        // Console.WriteLine(value3);
        
        
        //将字符串 TryParse() 为 int
        // string value4 = "20.597949";
        // decimal res = 0m;
        // if (decimal.TryParse(value4, out res))
        // {
        //     Console.WriteLine($"The result is {res}");
        // }
        // else
        // {
        //     Console.WriteLine($"死了");
        // }
        //
        // if (res>0)
        // {
        //     Console.WriteLine($"{50+res}");
        // }
        
        
        //练习 完成将字符串数组值合并为字符串和整数的挑战
        // string[] values = { "12.3", "45", "ABC", "11", "DEF" };
        // decimal Total = 0m,Number=0m;
        // string strs = "";
        // foreach (string s3 in values)
        // {
        //       if (decimal.TryParse(s3,out Number)) 
        //       { Total+=Number; }
        //       else
        //       {
        //           strs += s3;
        //       }
        // }
        // Console.WriteLine($"Message:{strs}");
        // Console.WriteLine($"Total:{Total}");
        
        
        //练习 - 完成将数学运算输出为特定数字类型的挑战
        /*int value1 = 11;
        decimal value2 = 6.2m;
        float value3 = 4.3f;
        
        Console.WriteLine($"Divide value1 by value2, display the result as an int: {Convert.ToInt32(value1/value2)}");
        Console.WriteLine($"Divide value2 by value3, display the result as a decimal: {value2/(decimal)value3}");
        Console.WriteLine($"Divide value3 by value1, display the result as a float: {value3/(float)value1}"); */

        // string[] pallets = [ "B14", "A11", "B12", "A13" ];

/*
        //  Sort() 和 Reverse()
        Console.WriteLine("Sorted...");
        Array.Sort(pallets);
        foreach (var pallet in pallets)
        {
            Console.WriteLine($"-- {pallet}");
        }

        Console.WriteLine("");
        Console.WriteLine("Reversed...");
        Array.Reverse(pallets);
        foreach (var pallet in pallets)
        {
            Console.WriteLine($"-- {pallet}");
        }*/
        
        // Array.Clear(pallets,0,2);
        // foreach (string VARIABLE in pallets)
        // {
        //     Console.WriteLine(VARIABLE);
        // }
        
        /*string[] pallets = [ "B14", "A11", "B12", "A13" ];
        Console.WriteLine("");
        
        Array.Clear(pallets, 0, 2);
        Console.WriteLine($"Clearing 2 ... count: {pallets.Length}");
        foreach (var pallet in pallets)
        {
            Console.WriteLine($"-- {pallet}");
        }
        Array.Resize(ref pallets ,6);
        pallets[4] = "C01";
        pallets[5] = "C02";
        foreach (string VARIABLE in pallets)
        {
            Console.WriteLine($"-- {VARIABLE}");
        }
        Array.Resize(ref pallets,3);
        foreach (string VARIABLE in pallets)
        {
            Console.WriteLine($"-- {VARIABLE}");
        }*/

        /*string result = "abc123";

        char[] ch = result.ToCharArray();
        Array.Reverse(ch);
        string res = string.Join(",",ch);
        Console.WriteLine(res);

        string[] strings = res.Split(",");
        foreach (string VARIABLE in strings)
        {
            Console.WriteLine($"variable: {VARIABLE}");
        }*/
        
        //练习 - 完成在句子中反向拼写单词的挑战
        /*string pangram = "The quick brown fox jumps over the lazy dog";

        string[] strings = pangram.Split(" ");

        string[] mes = new string[strings.Length];
        
        for (int i = 0; i < strings.Length; i++)
        {
            char[] ch = strings[i].ToCharArray();
            Array.Reverse(ch);
            mes[i] = new string(ch);
        }

        string join = string.Join(" ",mes);
        Console.WriteLine(join);*/
        
        //练习 - 完成一项分析订单字符串、对订单进行排序并标记可能错误的挑战
        /*string[] orderStream = "B123,C234,A345,C15,B177,G3003,C235,B179".Split(",");
        for (int i = 0; i < orderStream.Length; i++)
        {
            if (orderStream[i].Length == 4)
            {
                Console.WriteLine(orderStream[i]);
        
            }
            else
            {
                Console.WriteLine($"{orderStream[i]}    -Error");
            }
        }*/

        // string first = "hello";
        // string second = "world";
        // Console.WriteLine(string.Format("{0} {1}", first, second));

        // decimal price = 123.45m;
        // int discount = 50;
        // Console.WriteLine($"Price: {price:C} (Save {discount:C})");
        
        // int invoiceNumber = 1201;
        // decimal productShares = 25.4568m;
        // decimal subtotal = 2750.00m;
        // decimal taxPercentage = .15825m;
        // decimal total = 3185.19m;
        //
        // Console.WriteLine($"Invoice Number: {invoiceNumber}");
        // Console.WriteLine($"    Product Shares: {productShares:N3}");
        // Console.WriteLine($"        Subtotal: {subtotal:C}");
        // Console.WriteLine($"            Tax Percentage: {taxPercentage:P2}");
        // Console.WriteLine($"        Total Tax Amount: {total:C}");
        
        
        /*string paymentId = "769C";
        string payeeName = "Mr. Stephen Ortega";
        string paymentAmount = "$5,000.00";

        var formattedLine = paymentId.PadRight(6);
        formattedLine += payeeName.PadRight(24);
        formattedLine += paymentAmount.PadLeft(10);

        Console.WriteLine("1234567890123456789012345678901234567890");
        Console.WriteLine(formattedLine);*/
        
        //练习 - 完成将字符串内插应用于套用信函的挑战
        /*string customerName = "Ms. Barros";

        string currentProduct = "Magic Yield";
        int currentShares = 2975000;
        decimal currentReturn = 0.1275m;
        decimal currentProfit = 55000000.0m;

        string newProduct = "Glorious Future";
        decimal newReturn = 0.13125m;
        decimal newProfit = 63000000.0m;

        // Your logic here

        Console.WriteLine();

        Console.WriteLine("Here's a quick comparison:\n");

        string comparisonMessage = "";

        
        // Your logic here
        comparisonMessage += currentProduct.PadRight(20);
        comparisonMessage += string.Format("{0:P}", currentReturn).PadRight(10);
        comparisonMessage += string.Format("{0:C}\n", currentProfit).PadRight(15);
        comparisonMessage += newProduct.PadRight(20);
        comparisonMessage += string.Format("{0:P}", newReturn).PadRight(10);
        comparisonMessage += string.Format("{0:C}", newProfit).PadRight(15);
        Console.WriteLine(comparisonMessage);*/
        
        /*string message = "What is the value <span>between the tags</span>?";

        //使用文化敏感比较
        int openingPosition = message.IndexOf("<span>", StringComparison.Ordinal);
        int closingPosition = message.IndexOf("</span>", StringComparison.Ordinal);

        openingPosition += 6;
        int length = closingPosition - openingPosition;
        Console.WriteLine(message.Substring(openingPosition, length));*/
        // string message = "hello there!";
        //
        // int first_h = message.IndexOf('h');
        // int last_h = message.LastIndexOf('h');
        //
        // Console.WriteLine($"For the message: '{message}', the first 'h' is at position {first_h} and the last 'h' is at position {last_h}.");
        
        /*string message = "(What if) there are (more than) one (set of parentheses)?";
        while (true)
        {
                    int openingPosition = message.IndexOf('(');
                    if (openingPosition==-1)
                    {
                        break;
                    }
                    openingPosition += 1;
                    int closingPosition = message.IndexOf(')');
                    int length = closingPosition - openingPosition;
                    Console.WriteLine(message.Substring(openingPosition, length));
                    message = message.Substring(closingPosition+ 1);
        }*/
        /*string message = "Help (find) the {opening symbols}";
        Console.WriteLine($"Searching THIS Message: {message}");
        char[] openSymbols = [ '[', '{', '(' ];
        int startPosition = 5;
        int openingPosition = message.IndexOfAny(openSymbols);
        Console.WriteLine($"Found WITHOUT using startPosition: {message.Substring(openingPosition)}");

        openingPosition = message.IndexOfAny(openSymbols, startPosition);
        Console.WriteLine($"Found WITH using startPosition {startPosition}:  {message.Substring(openingPosition)}");*/
        
        
        //练习 - 完成从输入字符串中提取、替换和删除数据的质询
        const string input = "<div><h2>Widgets &trade;</h2><span>5000</span></div>";

        string quantity = "";
        string output = input; // 基于 input 逐步修改

// 1. 提取数量
        int spanStart = output.IndexOf("<span>", StringComparison.Ordinal) + "<span>".Length;
        int spanEnd = output.IndexOf("</span>", StringComparison.Ordinal);
        quantity = output.Substring(spanStart, spanEnd - spanStart);
        Console.WriteLine($"Quantity: {quantity}"); // 5000

// 2. 替换商标符号
        output = output.Replace("&trade;", "&reg;");

// 3. 删除开始的 <div> 标签
        int divStart = output.IndexOf("<div>", StringComparison.Ordinal);
        output = output.Remove(divStart, "<div>".Length); // 删除5个字符

// 4. 删除结尾的 </div> 标签
        int divEnd = output.IndexOf("</div>", StringComparison.Ordinal);
        output = output.Remove(divEnd, "</div>".Length); // 删除6个字符

        Console.WriteLine($"Output: {output}");
// 结果: <h2>Widgets &reg;</h2><span>5000</span>
    }
}